﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Controls;
namespace WPFExcelReport.Helpers
{
    /// <summary>
    /// VisualHelper convert the given visual content as an image
    /// </summary>
    public static partial class Helper
    {
        public static string GetDDMMYY(string YYYYMMDD)
        {
            //VKPLIEFTERM,VKPVERZTERM1,VKPVERZTERM2,VKPVERZTERM3,VKPVERZTERM4,VKPVERZTERM5对应的格式为YYYYMMDD
            string date = "";
            if (YYYYMMDD.Trim().Length < 8)
                return "        ";
            date = YYYYMMDD.Substring(6, 2) + "." + YYYYMMDD.Substring(4, 2) + "." + YYYYMMDD.Substring(2, 2);
            return date;
        }

        public static string GeStrDate(string src)
        {
            string str1 = "";
            if (src.Trim().Length != 8)
            {
                str1 = src;
            }
            else
            {
                str1 = src.Substring(6, 2) + "." + src.Substring(4, 2) + "." + src.Substring(2, 2);
            }
            return str1;

        } 

        public static void GetDate(ref string date, ref string year, ref string month, ref string day)
        {
            DateTime now = DateTime.Now;
            year = now.Year.ToString();
            month = Right("00" + now.Month.ToString(), 2);
            day = Right("00" + now.Day.ToString(), 2);
            date = year + month + day;
        }

        public static void GetTime(ref string date, ref string time)
        {
            DateTime now = DateTime.Now;
            string year = now.Year.ToString();
            string month = Right("00" + now.Month.ToString(), 2);
            string day = Right("00" + now.Day.ToString(), 2);
            string hh = Right("00" + now.Hour.ToString(), 2);
            string mm = Right("00" + now.Minute.ToString(), 2);
            string ss = Right("00" + now.Second.ToString(), 2);
            string ms = Right("00" + now.Millisecond.ToString(), 2);
           
            date = year + month + day;
            time = hh + mm + ss + ms; 
        }
        public static void GetTime(ref string date, ref string time,ref long ticks )
        {
            DateTime now = DateTime.Now;
            ticks = now.Ticks;
            string year = now.Year.ToString();
            string month = Right("00" + now.Month.ToString(), 2);
            string day = Right("00" + now.Day.ToString(), 2);
            string hh = Right("00" + now.Hour.ToString(), 2);
            string mm = Right("00" + now.Minute.ToString(), 2);
            string ss = Right("00" + now.Second.ToString(), 2);
            string ms = Right("00" + now.Millisecond.ToString(), 2);

            date = year + month + day;
            time = hh + mm + ss + ms;
        }

        public static void GetSTime(ref string date, ref string time, ref string stime)
        {
            DateTime now = DateTime.Now;
            string year = now.Year.ToString();
            string month = Right("00" + now.Month.ToString(), 2);
            string day = Right("00" + now.Day.ToString(), 2);
            string hh = Right("00" + now.Hour.ToString(), 2);
            string mm = Right("00" + now.Minute.ToString(), 2);
            string ss = Right("00" + now.Second.ToString(), 2);
            string ms = Right("00" + now.Millisecond.ToString(), 2);

            date = year + month + day;
            time = hh + mm + ss + ms;
            stime = hh + mm;
        } 

        public static void GetSplitDate(ref string str1, ref string str2, ref string str3, string src)
        {
            if (src.Trim().Length != 8)
            {
                str1 = src;
                str2 = src;
                str3 = src;
                return;
            }
            str1 = src.Substring(0, 4) + "年" + src.Substring(4, 2) + "月" + src.Substring(6, 2) + "日";
            str3 = src.Substring(0, 4) + "/" + src.Substring(4, 2) + "/" + src.Substring(6, 2);
            str2 = src.Substring(4, 2) + "月" + src.Substring(6, 2) + "日";
        }

        public static void SplitDate(ref int year, ref int month, ref int day, string date)
        {
            if (date.Trim().Length != 8)
            {
                year = 0;
                month = 0;
                day = 0;
                return;
            }
            year = Convert.ToInt16(date.Substring(0, 4));
            month = Convert.ToInt16(date.Substring(4, 2));
            day = Convert.ToInt16(date.Substring(6, 2));
        }

        public static void SplitTime(ref int hh, ref int mm, string time)
        {
            if (time.Trim().Length != 4)
            {
                hh = 0;
                mm = 0; 
                return;
            }
            hh = Convert.ToInt16(time.Substring(0, 2));
            mm = Convert.ToInt16(time.Substring(2, 2)); 
        }

        public static void GetStdDate(ref string date,ref string time,DateTime datetime)
        {
             date = datetime.Year +Right("00"+ datetime.Month,2) +Right("00"+ datetime.Day,2);
             time =  Right("00" + datetime.Hour, 2) + Right("00" + datetime.Minute, 2);
        }

        public static void GetDateTime(ref DateTime dated, string date, string time)
        {
             int year=0,   month=0,   day=0,hh=0,mm=0;
             if (date == "0")
             {
                 dated= System.DateTime.Now;
                 return;
             }
            SplitDate(ref year,ref month,ref day,date);
            SplitTime(ref hh,ref mm,time);
            dated = new DateTime(year, month, day, hh, mm, 0);
        }

        public static string GetLastDayOfMonth(int Year, int iMonth)
        {

            DateTime dtTo = new DateTime(Year, iMonth, 1); 
            dtTo = dtTo.AddMonths(1);  
            dtTo = dtTo.AddDays(-(dtTo.Day));
            string date = Year + Right("00" + iMonth, 2) + Right("00" + dtTo.Day, 2);
            return date; 
        }
        public static string GetYesterday()
        { 
            DateTime now = System.DateTime.Now;
            DateTime yesterday = now.AddDays(-1); 
            return yesterday.Year + Right("00" + yesterday.Month, 2) + Right("00" + yesterday.Day, 2);
        }
        public static string GetPreviousday(int days)
        {
            DateTime now = System.DateTime.Now;
            DateTime yesterday = now.AddDays(days);
            return yesterday.Year + Right("00" + yesterday.Month, 2) + Right("00" + yesterday.Day, 2);
        }

        public static string GetDate(object sender)
        {
              
            DatePicker snder = (DatePicker)sender;
            DateTime date = (DateTime)snder.SelectedDate;
            string outdate = "";
            try
            {

                if (date != null)
                {
                    string year = date.Year.ToString();
                    string month = Right("00" + date.Month.ToString(), 2);
                    string day = Right("00" + date.Day.ToString(), 2);
                    outdate = year + month + day;
                }
            }

            catch (Exception ex)
            {
               
            }
            return outdate;
        }

        public static void GetLastMonth(ref string LastMonth,ref string Begin,ref string End)
        {
            DateTime now = System.DateTime.Now;
            int year = 2015, month = 1, day = 1;
            if (now.Month == 1)
            {
                year = now.Year - 1;
                month = 12;
                day = 1;
                LastMonth = year + Right("00" + month, 2);
                Begin = LastMonth + Right("00" + day, 2);
                End=GetLastDayOfMonth(year, month); 
            }
            else
            {
                year = now.Year;
                month = now.Month-1;
                day = 1;
                LastMonth = year + Right("00" + month, 2);
                Begin = LastMonth + Right("00" + day, 2);
                End = GetLastDayOfMonth(year, month); 
            }         
        }

        public static void GetLastMonth(string date,ref string LastMonth, ref string Begin, ref string End)
        {
            if (date.Length < 6)
                return;
            int year =Convert.ToInt16( date.Substring(0, 4));
            int month =Convert.ToInt16( date.Substring(4, 2));
            int day = 1;
            if (month == 1)
            {
                year = year - 1;
                month = 12;
                day = 1;
                LastMonth = year + Right("00" + month, 2);
                Begin = LastMonth + Right("00" + day, 2);
                End = GetLastDayOfMonth(year, month);
            }
            else
            { 
                month = month - 1;
                day = 1;
                LastMonth = year + Right("00" + month, 2);
                Begin = LastMonth + Right("00" + day, 2);
                End = GetLastDayOfMonth(year, month);
            }
        }

    }
}
